package cn.mothin.service.common;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mothin.bean.easyui.DataContent;
import cn.mothin.dao.OrganizationDao;
import cn.mothin.dao.SystemUserDao;
import cn.mothin.dictionary.SystemUserState;
import cn.mothin.entity.Organization;
import cn.mothin.entity.SystemUser;
import cn.mothin.frame.core.base.BaseService;
import cn.mothin.frame.util.StringUtil;
import cn.mothin.vo.common.SelectorForm;

import com.google.gson.Gson;

@Service("54276d117eafacd8117c2a4f")
public class SelectorService extends BaseService {
    @Autowired
    private SystemUserDao systemUserDao;
    @Autowired
    private OrganizationDao organizationDao;

    /**
     * 添加机构下的用户信息
     * 
     * @param parent
     */
    private void addOrganizationUser(DataContent parent,List<String> selected) {
        String sql = "SELECT * FROM system_user WHERE deleted<>TRUE AND superUser<>TRUE AND state=? AND organizationId = ? ORDER BY name ";
        List<SystemUser> systemUsers = systemUserDao.query(sql,
                SystemUserState.NORMAL.getId(), parent.getId());
        for (int i = 0; i < systemUsers.size(); i++) {
            SystemUser systemUser = systemUsers.get(i);
            DataContent dataContent = new DataContent();
            dataContent.setId(systemUser.getId());
            dataContent.setText(systemUser.getName());
            if(selected.size()>0&&selected.contains(systemUser.getId())){
                dataContent.setChecked(true);
            }
            dataContent.getAttributes().put("targetType", "1");
            parent.addChild(dataContent);
        }
    }

    private void addSub(DataContent parent,List<String> selected) {
        String sql = "SELECT * FROM organization WHERE deleted<>TRUE AND parentId = ? ORDER BY organizationName";
        List<Organization> organizations = organizationDao.query(sql,
                parent.getId());
        for (int i = 0; i < organizations.size(); i++) {
            Organization organization = organizations.get(i);
            DataContent dataContent = new DataContent();
            dataContent.setId(organization.getId());
            dataContent.setText(organization.getOrganizationName());
            dataContent.getAttributes().put("targetType", "0");
            dataContent.setStateClosed(true);
            addSub(dataContent,selected);
            addOrganizationUser(dataContent,selected);
            parent.addChild(dataContent);
        }
    }

    public String querySystemUserJson(SelectorForm form) {
        List<DataContent> list = new ArrayList<DataContent>();
        List<String> selected=new ArrayList<String>();
        if(!StringUtil.isNullOrEmpty(form.getSelected())){
            String[] ss=form.getSelected().split(",");
            for(int i=0;i<ss.length;i++){
                selected.add(ss[i]);
            }
        }

        String sql = "SELECT * FROM organization WHERE deleted<>TRUE AND parentId IS NULL ORDER BY organizationName";
        List<Organization> organizations = organizationDao.query(sql);
        for (int i = 0; i < organizations.size(); i++) {
            Organization organization = organizations.get(i);
            DataContent dataContent = new DataContent();
            dataContent.setText(organization.getOrganizationName());
            dataContent.setId(organization.getId());
            dataContent.getAttributes().put("targetType", "0");
            dataContent.setStateClosed(true);
            addSub(dataContent,selected);
            addOrganizationUser(dataContent,selected);
            list.add(dataContent);
        }
        Gson gson = new Gson();
        return gson.toJson(list);
    }
}
